package com.example.shop.goods.dto;

import com.example.shop.goods.enums.GoodsRangeFieldType;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.jdbc.SQL;

import javax.validation.constraints.NotNull;

@Data
public class RangeQueryParam {
    //该RangeQueryParam对应的sql语句片段
    public static final String RANGE_SQL =
        " " +
        "<choose>" +
            "<when test = 'ew.rangeQueryParams != null and !ew.rangeQueryParams.isEmpty() '>" +
                "<foreach collection='ew.rangeQueryParams' item='item' index='index' separator=' and ' open = ' ' close = ' '>" +
                    "( " +
                    "<choose>" +
                        "<when test= 'item.lowerLimitInclude'>"+
                            "${item.fieldType.getDbField()} &gt;= ${item.lowerLimit}" +
                        "</when>" +
                        "<otherwise>" +
                            "${item.fieldType.getDbField()} &gt; ${item.lowerLimit}" +
                        "</otherwise>" +
                    "</choose>" +
                    " and " +
                    "<choose>" +
                        "<when test= 'item.upperLimitInclude'>"+
                            "${item.fieldType.getDbField()} &lt;= ${item.upperLimit}" +
                        "</when>" +
                        "<otherwise>" +
                            "${item.fieldType.getDbField()} &lt; ${item.upperLimit}" +
                        "</otherwise>" +
                    "</choose>" +
                    " )" +
                "</foreach>" +
            "</when>"+
            "<otherwise>" +
                " 1 "+
            "</otherwise>" +
        "</choose>" +
        " ";

    //要范围的字段
    @NotNull
    protected GoodsRangeFieldType fieldType;
    //包含下限值，默认是包含
    protected boolean lowerLimitInclude =  true;
    //下限，默认无穷下
    protected long lowerLimit = Long.MIN_VALUE;
    //下限
    //包含上限值，默认是包含
    protected boolean upperLimitInclude = true;
    //上限，默认无穷大
    protected long upperLimit = Long.MAX_VALUE;

    public RangeQueryParam(){
    }


    public static Builder builder(){
        return new Builder();
    }

    public static class Builder{
        protected RangeQueryParam rangeQueryParam;

        public Builder(){
            rangeQueryParam = new RangeQueryParam();
        }
        public RangeQueryParam build(){
            return rangeQueryParam;
        }

        public Builder setFieldType(GoodsRangeFieldType fieldType) {
            rangeQueryParam.fieldType = fieldType;
            return this;
        }

        public Builder setLowerLimitInclude(boolean lowerLimitInclude) {
            rangeQueryParam.lowerLimitInclude = lowerLimitInclude;
            return this;
        }

        public Builder setLowerLimit(Long lowerLimit) {
            rangeQueryParam.lowerLimit = lowerLimit;
            return this;
        }

        public Builder setUpperLimitInclude(Boolean upperLimitInclude) {
            rangeQueryParam.upperLimitInclude = upperLimitInclude;
            return this;
        }

        public Builder setUpperLimit(Long upperLimit) {
            rangeQueryParam.upperLimit = upperLimit;
            return this;
        }

    }
}
